package com.bjpowernode.jdbc;

import java.sql.*;

public class JDBCTest9 {
    public static void main(String[] args) {
        //用PreparedStatement实现增删改
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode", "root", "246810");
            //3.获取预编译数据库操作对象
            //增
            /*String sql = "insert into dept(deptno,dname,loc) values (?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,60);
            ps.setString(2,"销售部");
            ps.setString(3,"上海");*/

            //改
            /*String sql = "update dept set dname = ? , loc = ? where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, "销售1部");
            ps.setString(2, "上海2");
            ps.setInt(3, 60);*/

            //删
            String sql = "delete from dept where deptno  = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, 60);

            //4.执行sql
            int count = ps.executeUpdate();
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
